package com.example.apple.weather;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.apple.weather.bean.WeatherInfo;
import com.example.apple.weather.iface.WeatherInterface;
import com.example.apple.weather.listener.WeatherListener;
import com.example.apple.weather.model.Weather51Model;
import com.example.apple.weather.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements WeatherListener,View.OnClickListener{
    private EditText cityNOInput;
    private TextView tv_cityname;
    private TextView tv_citynumber;
    private TextView tv_wd;
    private TextView tv_fx;
    private TextView tv_fl;
    private TextView tv_sd;
    private Button btn_find1;
    private Button btn_find2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init(){
        cityNOInput =(EditText) findViewById(R.id.et_citynumber);
        btn_find1 =(Button) findViewById(R.id.button);
        btn_find2 =(Button) findViewById(R.id.button2);
        tv_cityname=(TextView) findViewById(R.id.tv_cityname);
        tv_citynumber=(TextView) findViewById(R.id.et_citynumber);
        tv_wd=(TextView) findViewById(R.id.tv_wd);
        tv_sd=(TextView) findViewById(R.id.tv_sd);
        tv_fl=(TextView) findViewById(R.id.tv_fl);
        tv_fx=(TextView) findViewById(R.id.tv_fx);
        btn_find1.setOnClickListener(this);
        btn_find2.setOnClickListener(this);
    }

    @Override
    public void onResponse(WeatherInfo weather) {
        if (weather!=null){
            tv_cityname.setText(weather.getCity());
            tv_citynumber.setText(weather.getCityid());
            tv_fl.setText(weather.getFl());
            tv_wd.setText(weather.getWd());
            tv_sd.setText(weather.getSD());
            tv_fx.setText(weather.getFx());
        }
        else{
            tv_cityname.setText("未知");
        }
    }

    @Override
    public void onFall(String msg) {
        tv_cityname.setText("失败");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                WeatherModel weatherModel=new WeatherModel();
                weatherModel.getWeather(cityNOInput.getText().toString(),this);
                break;
            case R.id.button2:
                Weather51Model.weather51Model=new Weather51Model();
                WeatherInterface weather51Model = null;
                weather51Model.getWeather(cityNOInput.getText().toString(),this);
                break;
            default:
                break;
        }
    }
}
